package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * 实现用户注册功能
 * 程序启动后要求用户顺序输入:用户名,密码,昵称,年龄
 * 用户名,密码,昵称,不能为空,年龄必须是整数
 * 然后将该信息插入到表userinfo中
 */
public class RegDemo {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");

        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3307/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
//        System.out.println("成功连接数据库!");

        Statement statement = connection.createStatement();

        UserInfo userInfo = InputUtil.getInputObject(new UserInfo(),"欢迎注册" ,"注册");
        System.out.println(userInfo);
        String sql = "INSERT INTO userinfo(username , password , nickname , age) VALUES ('"
                +userInfo.getUsername()+"','"
                +userInfo.getPassword()+"','"
                +userInfo.getNickname()+"',"
                +userInfo.getAge()+")";
        int count = statement.executeUpdate(sql);
        if (count>0){
            System.out.println("注册成功");
        }else{
            System.out.println("注册失败");
        }
        statement.close();
    }
}